import { db, STORES } from './db'

// 获取用户的每日计划
export const getUserDailyPlans = async (userEmail, weekDay) => {
  try {
    const plans = await db.getAll(STORES.DAILY_PLANS)
    return plans.filter(plan => 
      plan.userEmail === userEmail && 
      plan.weekDay === weekDay
    )
  } catch (error) {
    console.error('获取每日计划失败:', error)
    return []
  }
}

// 添加新的每日计划
export const addDailyPlan = async (plan) => {
  try {
    await db.add(STORES.DAILY_PLANS, {
      ...plan,
      userEmail: plan.userEmail,
      date: plan.date,
      weekDay: plan.weekDay,
      done: false,
      timestamp: Date.now()
    })
    return true
  } catch (error) {
    console.error('添加每日计划失败:', error)
    return false
  }
}

// 更新计划状态
export const updatePlanStatus = async (planId, done) => {
  try {
    const plan = await db.get(STORES.DAILY_PLANS, planId)
    if (plan) {
      plan.done = done
      await db.put(STORES.DAILY_PLANS, plan)
      return true
    }
    return false
  } catch (error) {
    console.error('更新计划状态失败:', error)
    return false
  }
}

// 删除计划
export const deleteDailyPlan = async (planId) => {
  try {
    await db.delete(STORES.DAILY_PLANS, planId)
    return true
  } catch (error) {
    console.error('删除计划失败:', error)
    return false
  }
} 